import factor_analyzer
import numpy as np
import pandas as pd
from sklearn.decomposition import PCA
from FirstExp import kam
import os
import tkinter as tk
from tkinter import *
from tkinter import ttk
from tkinter import filedialog
from PyQt5.QtWidgets import QWidget, QLabel, QComboBox, QApplication
import sys
from factor_analyzer import FactorAnalyzer


# mat = np.array([[-1, 1, 0],
#                 [-4, 3, 0],
#                 [1, 0, 0]])
#
# eigenvalue, featurevector = np.linalg.eig(mat)

# print("特征值：", eigenvalue)
# print("特征向量：", featurevector)

# arr = np.array([[1, 2, 3], [8, 9, 12]])
# 1 2 3 4 5 6 7 8 9 10 1 2 1 3 14 12 12 14 1 4 5 7 8 9 23
place = input("")
temp = [int(n) for n in place.split()]
if len(temp) < 25:
    print("输入有误！")
arr = np.matrix(temp).reshape(5, 5)
if arr.dtype != 'int32':
    print("输入有误！")
print(np.linalg.eigvals(arr))
eigenvalue, featurevector = np.linalg.eigvalsh(arr, UPLO='L')

# print(eigenvalue)
print(featurevector)
print(featurevector * np.diag(eigenvalue) * np.linalg.inv(featurevector))

# place = input("")
# temp = [float(n) for n in place.split()]
# arr = np.array(temp).reshape(2, 31)
# print(np.corrcoef(arr))

# file_obj = open('testx.csv', encoding='UTF-8')
# 转化为所有的行组成的列表
# fileList = file_obj.readlines()
# print((file_obj)
# 获取行数
# line = len(fileList)
# 生成一个line行，4列的零矩阵
# returnMat = np.zeros(line, 4)
# df = kam.readfile()
# print(df)

class KamData:
    """
    Parameters
        ----------
        data : 数据部分
        path : 路径
    """

    def __init__(self, data, path):
        np.array(data)
        self.data = data
        self.path = path


li = [[1, 1], [1, 3], [2, 3], [4, 4], [2, 4]]
matrix = np.mat(li)
# 求均值
mean_matrix = np.mean(matrix, axis=0)
# 减去平均值
Dataadjust = matrix - mean_matrix
# print(Dataadjust.shape)
# 计算特征值和特征向量
covMatrix = np.cov(Dataadjust, rowvar=False)
eigValues, eigVectors = np.linalg.eig(covMatrix)
# print(eigValues.shape)
# print(eigVectors.shape)
# 对特征值进行排序
eigValuesIndex = np.argsort(eigValues)
# print(eigValuesIndex)
# 保留前K个最大的特征值
eigValuesIndex = eigValuesIndex[:-(1000000):-1]
# print(eigValuesIndex)
# 计算出对应的特征向量
trueEigVectors = eigVectors[:, eigValuesIndex]
# print(trueEigVectors)
# 选择较大特征值对应的特征向量
maxvector_eigval = trueEigVectors[:, 0]
# print(maxvector_eigval)
# 执行PCA变换：Y=PX 得到的Y就是PCA降维后的值 数据集矩阵
pca_result = maxvector_eigval.dot(Dataadjust.T)
fa = factor_analyzer.FactorAnalyzer
